package photogcalc;

import java.io.IOException;
import java.util.Vector;
import photogcalc.common.Constants;
import photogcalc.i18n.I18n;
import photogcalc.i18n.I18nFactory;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Screen;
import net.sf.microlog.core.Logger;
import net.sf.microlog.core.LoggerFactory;
import photogcalc.calculators.CalculatorController;
import photogcalc.util.ImageProxy;

/**
 *
 * @author aokkonen
 */
public class MainView implements CommandListener {
    private Command exitCmd;
    private Command deleteRSCmd;
    //private Command showLogCmd;
    private Command selectCmd;
    private Command propertiesCmd;
    private Command aboutCmd;

    private I18n i18n = null;
    private MainCommandListener listener = null;

    private List calculatorsList = null;
    private Screen mainForm = null;

    private Logger logger = LoggerFactory.getLogger("MainView");

    /** Default constructor. */
    public MainView(MainCommandListener listener) {
        this.listener = listener;

        i18n = I18nFactory.createI18n(System.getProperty("microedition.locale"));

        mainForm = createUI();
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCmd) {
            listener.exitCommand();
        }
        else if (c == deleteRSCmd) {
            listener.deleteRecordStoreCommand();
        }
        else if (c == selectCmd) {
            listener.calculatorCommand(calculatorsList.getSelectedIndex());
        }
//        else if (c == showLogCmd) {
//            settings.saveRecords();
//            displayLogForm();
//        }
        else if (c == propertiesCmd) {
            listener.propertiesCommand();
        }
        else if (c == aboutCmd) {
            listener.aboutCommand();
        }
        else {
            logger.warn("Unknown command " + c.getLabel());
        }
    }

    /** Creates and returns the main form
        @return the main form and it's menu
    */
    protected Screen createUI() {
        //Form form = new Form(Constants.TITLE);

        calculatorsList = new List(
                Constants.TITLE,
                List.IMPLICIT);
        calculatorsList.setFitPolicy(List.TEXT_WRAP_ON);
        
        // Add commands to menu
        selectCmd = new Command(i18n.get("_Valitse"), Command.ITEM, 1);
        calculatorsList.addCommand(selectCmd);
        calculatorsList.setSelectCommand(selectCmd);
        
        if (Constants.APPLET_MODE == false) {
            exitCmd = new Command(i18n.get("_Sulje"), Command.EXIT, 2);
            calculatorsList.addCommand(exitCmd);
        }
        if (Constants.APPLET_MODE == false &&
            Constants.PROPERTIES_FORM == true) {
            propertiesCmd = new Command(i18n.get("_Asetukset"), Command.ITEM, 3);
            calculatorsList.addCommand(propertiesCmd);
        }

//        showLogCmd = new Command(i18n.get("_Nayta loki"), Command.ITEM, 4);
//        form.addCommand(showLogCmd);

        aboutCmd = new Command(i18n.get("_Ohjelmatiedot"), Command.ITEM, 4);
        calculatorsList.addCommand(aboutCmd);

//        deleteRSCmd = new Command(
//                i18n.get("_Poista asetukset"),
//                i18n.get("_Poista asetukset ja poistu"),
//                Command.ITEM, 5);
//        form.addCommand(deleteRSCmd);

        calculatorsList.setCommandListener(this);

        return calculatorsList;
    }

    public void initData(Vector calculators) {
        ImageProxy proxy = new ImageProxy();

        for (int i = 0; i < calculators.size(); i++)
            try {
                calculatorsList.insert(
                    i,
                    i18n.get(((CalculatorController)calculators.elementAt(i)).getMenuName()),
                    proxy.createImage(((CalculatorController)calculators.elementAt(i)).getIconPrefix())
                );
            }
            catch (IOException e) { 
                logger.warn(e);
            }
    }
    
    public Screen getScreen() {
        return mainForm;
    }
}
